<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserController
 *
 * @author MinhNguyen
 */
class UserController {

    private $model;
    private $view;
    
    public function __construct($model, $view) {
        $this->model = $model;
        $this->view = $view;
    }
    
    /**
     * Login
     * @param String $password
     */
    public function login($password) {
        if ($password != null)
            if ($password == $this->model->password) {
                $this->view->status = new Status();
                $this->view->status->code = 200;
                $this->view->status->message = "OK";
                $this->view->result = $this->model;
                $this->view->display("Master/jsonView.php");
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView(), 
                    "401", "Wrong password");
                $controller->returnError();
            }
    }
    
    /**
     * To save User to Database 
     * if it's New User, insert User to Users table
     * if is's Old User, update it 
     */
    public function Save() {
        $model = $this->model;
        $query = "SELECT * FROM users WHERE userid = $model->id";
        $result = Database::getInstance()->query($query);
        if ($result->returnCode == 200)  //OK
            if ($result->dataTable->numOfRow > 0) {
                $query = "UPDATE users
                        SET fullname='$model->name', 
                        password='$model->password', 
                        email='$model->email', 
                        phone='$model->phone', 
                        address='$model->address', 
                        company='$model->company', 
                        position='$model->position', 
                        dateregistered ='$model->dateRegistered', 
                        token ='$model->token',
                        managerid = '$model->managerId'
                        WHERE userid = $model->id";
                Database::getInstance()->query($query);
                return true;
            }
            else {
                Database::getInstance()->query("INSERT INTO Users 
                            (email, password, fullname, company, address, phone, position, dategistered, token, timezone, managerid) VALUES
                            ('$model->email', '$model->password',
                            '$model->fullname', '$model->company', 
                            '$model->address', '$model->phone', 
                            '$model->position', '$model->dateRegistered', 
                            '$model->token', '$model->timezone', 
                            '$model->managerId')");
                return true;
            }
    }
    
    /**
     * To Delete User by own in Users Table (database)
     */
    public function Delete() {
        $model = $this->model;
        $query = "DELETE FROM Users WHERE ID = $model->id";
        Database::getInstance()->query($query);
        return true;
    }
}
?>
